<template>
  <div class="container">
    <div class="tip">
      <p class="one">公钥</p>
      <p class="two">私钥</p>
    </div>

    <div class="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input v-model="publicKey" type="textarea" rows="20" placeholder="公钥" resize="none" />
        </el-col>
        <el-col :span="12">
          <el-input v-model="privateKey" type="textarea" rows="20" placeholder="私钥" resize="none" />
        </el-col>
      </el-row>
    </div>

    <div class="bottom">
      <BothInputTemplate v-model:leftStr="originStr" v-model:rightStr="encryptRes">
        <template #menu>
          <div class="menu flex-column-center">
            <el-button type="primary" size="small" @click="onEncrypt">
              加 密
              <i class="iconfont icon-jiami1"></i>
            </el-button>

            <el-button type="primary" size="small" @click="onDecode">
              解 密
              <i class="iconfont icon-jiemi"></i>
            </el-button>

            <el-button type="success" size="small" :disabled="!encryptRes" @click="useCopy(encryptRes)">
              复制结果
              <i class="iconfont icon-fuzhi"></i>
            </el-button>
          </div>
        </template>
      </BothInputTemplate>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import md5 from "@/utils/md5";
import useCopy from "@/hook/useCopy";
import { DefaultPublicKey, DefaultPrivateKey } from "./key";
import { message } from "@/utils/Element";
import JSEncrypt from "@/utils/Class/JSEncrypt";

const originStr = ref("");
const encryptRes = ref("");

const publicKey = ref(DefaultPublicKey);
const privateKey = ref(DefaultPrivateKey);

// 加密方法
const encrypt = (pas: string) => {
  // const jse = new window.JSEncrypt();
  const jse = new JSEncrypt();
  jse.setPublicKey(publicKey.value);
  return jse.encrypt(pas);
};

// 解密
const decrypt = (pas: string) => {
  // const jse = new window.JSEncrypt();
  const jse = new JSEncrypt();
  jse.setPrivateKey(privateKey.value);
  return jse.decrypt(pas);
};

const onEncrypt = () => {
  try {
    const res = encrypt(originStr.value);
    if (encryptRes.value !== res) {
      encryptRes.value = res;
    }
  } catch (error: unknown) {
    if (error instanceof Error) {
      message.error(error.message);
    }
  }
};

const onDecode = () => {
  try {
    const res = decrypt(originStr.value);
    if (encryptRes.value !== res) {
      encryptRes.value = res;
    }
  } catch (error: unknown) {
    if (error instanceof Error) {
      message.error(error.message);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "@/styles/var.scss";
.container {
  .tip {
    display: flex;
    justify-content: space-between;
    font-weight: bold;
    padding: 0 10px;
    font-size: 14px;
    margin-bottom: 10px;
    color: rgb(87, 86, 86);
    .one {
      color: $success;
    }
    .two {
      color: $danger;
    }
  }

  .top {
    margin-bottom: 20px;
  }
  .menu {
    :deep(.el-button) {
      width: 70%;
      margin: 6px 0;
      i {
        margin-left: 6px;
      }
    }
  }
}
</style>
